// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const G = require("./util/g");
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        fly_anima: null,
        rigibody: null,
        timer: null
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.rigibody = this.node.getComponent(cc.RigidBody);
        this.fly_anima = this.getComponent(cc.Animation);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        this.timer = setInterval(() => {
            this.fly_anima.play();
        }, 1000);
    },

    start() {

    },

    update(dt) {
        if (this.node.y < -320) {
            G.game_over = true;
        }
        if (G.game_over) {
            this.node.awake = false;
        }
    },

    onKeyDown: function (e) {
        switch (e.keyCode) {
            case cc.macro.KEY.space:
                if (G.game_over) this.restart();
                if (G.start) this.jump();
                else if (!G.start) this.startPlay();
                break;
        }
    },

    startPlay() {
        clearInterval(this.timer);
        this.node.awake = true;
        G.start = true;
        this.jump();
        G.createPipe();
    },

    restart() {
        G.score.string = 0;
        G.pipe_exist = 0;
        G.game_over = false;
        G.over_lab.active = false;
        this.node.awake = true;
        this.node.x = 0;
        this.node.y = 0;
        G.start = true;
        G.clearPipe();
        G.pipe1_pool.clear();
        G.pipe2_pool.clear();
        G.createPipe();
        // console.log(this.pipe1_pool);
        // console.log(this.pipe2_pool);
    },

    jump: function (e) {
        // console.log(111, e)
        // G.getOnePoint();
        this.fly_anima.stop();
        if (this.node.children[2].active) {
            this.node.children[1].active = true;
            this.node.children[2].active = false;
        }
        this.fly_anima.play();
        let v = this.rigibody.linearVelocity;
        v.y = 300;
        this.rigibody.linearVelocity = v;
    },

    onBeginContact: function (contact, selfCollider, otherCollider) {
        this.fly_anima.stop();
        this.node.children[0].active = true;
        this.node.children[1].active = false;
        this.node.children[2].active = false;
        console.log('碰撞了');
        G.game_over = true;
    },

});
